package pl.donajski.worldBrowser.services;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import pl.donajski.worldBrowser.entities.City;
import pl.donajski.worldBrowser.entities.Country;
import pl.donajski.worldBrowser.primefaces.CountryModel;
import pl.donajski.worldBrowser.services.dao.IWorldDAO;

public class Countries implements Serializable {

	private static final long serialVersionUID = 1L;
	

	private IWorldDAO worldDao;
	private Country selectedCountry;
	private List<Country> countries;
	private List<City> selectedCityCountries;

	@PostConstruct
	public void init() {
		this.countries = this.worldDao.getAllCountries();
    }
	
	/**
	 * @param worldDao the worldDao to set
	 */
	@Resource(name="worldBrowserDao")
	public void setWorldDao(IWorldDAO worldDao) {
		this.worldDao = worldDao;
	}
	
	public List<Country> getCountries() {
		return countries;
	}
	
	public CountryModel getCountryModel() {
		return new CountryModel(this.countries);
	}
	
	public Country getSelectedCountry() {
		return selectedCountry;
	}

	public void setSelectedCountry(Country selectedCountry) {
		this.selectedCountry = selectedCountry;
	}

	public void setCountries(List<Country> countries) {
		this.countries = countries;
	}
	
	public List<City> getCities() {
		return selectedCityCountries = this.worldDao.getCitiesByCountryCode(selectedCountry.getCode());
	}
	
	public int getRegionPopulation(String region) {
		int sum = 0;
		for(City c : selectedCityCountries) {
			if(c.getDistrict().equals(region))
				sum += c.getPopulation();
		}
		
		return sum;
	}
}
